var add,edit,setVal,reset;
require(['select2','date','layout','validator', 'ajaxfileupload'], function() {
	var $formData = $('#formData');
    // var src = "../../../"+$.getProjectName()+"/content/images/add-icon1_25.png";
    var src = "";
    // $("#img").attr("src",src);
	$formData.initForm();
	$.getProjectName()
	add = function(fn) {
		addOrUpdate("/baseDrvInfo/addDriverInfo.htm",fn);
	};
	edit = function(fn) {
		addOrUpdate("/baseDrvInfo/uptDriverInfo.htm",fn);
	};

	setVal = function (drvId){
		$.ajaxExt({
			url :"/baseDrvInfo/getDriverInfoByDrvId.htm",
			data : {'drvId' : drvId}
		}).done(function(data){
            $("#img").attr("src","");
            if(typeof (data.drvHeadPath) != 'undefined'){
                $("#img").attr("src",$.getProjectName() + "/baseBlackArchivesInfo/readImage.htm?uploadPath=" + data.drvHeadPath);
            }
			// console.info(data);
			$formData.setValues(data);
		});
	};
	
	reset = function (){
        $("#img").attr("src","");
        // $("#img").attr("src",src);
		$formData.reset($formData);
	};
	
	function addOrUpdate(url,callBack){
        // $("#img").attr("src",src);
		var result = $.validator($formData);
		$.ajaxExt({
			url: '/baseOrgan/getBaseOrganList.htm'
		}).done(function(data){
			$.each(data, function(i, res){
				if($("input[name='organId']").val() == res.organId){
					$("input[name='corpId']").val(res.corpId);
					if(result){
						$.ajaxExt({
							url : url,
							data : $formData.getValues()
						}).done(function(){
							callBack();
						});
					}
					return;
				}
			});
		});
		
	}

	//点击弹出上传文件框
    $('#img').on('click', function () {
        console.info(11111);
        $("#driverHead").click();
    });
	//点击清除图片
	$('#emptyImg').on('click',function(){
		$('#img').attr("src","");
		$('#drvHeadPath').val("");
	});
	//上传文件
	window.upload = function(el){
		var values = $formData.getValues()
        values.uploadPath = '/files/upload/driverHead/';
    	$.ajaxFileUpload({
    		url:$.getProjectName()+"/baseDrvInfo/uploadDriverHead.htm",//需要连接到的服务器地址
            secureuri: false,
			fileElementId:'driverHead',
			data:values,//需要传的参数
			dataType:'text',
			success:function(data){
				//判断是否IE浏览器
				var userAgent = navigator.userAgent;//取得浏览器的userAgent字符串
				var isEdge = userAgent.indexOf("Edge") > -1;//判断是否IE的Edge浏览器
				var isFF = userAgent.indexOf("Firefox") > -1;
				if(isEdge || isFF){
                    if(data.length == 54){
                        var data1 = JSON.parse(data);
                        $.messager.alert("错误", data1.msg, "error");
                    }
                    $('#img').attr("src", $.getProjectName() + "/baseBlackArchivesInfo/readImage.htm?v="+Math.random()+"&uploadPath=" + data.substring(2));
                    $('#drvHeadPath').val(data.substring(2));
                }else if(data.indexOf("pre") > 0){
                    console.info(1);
                    data = data.replace(/<\/*?pre[^<>]*>/, '');  //ajaxFileUpload会对服务器响应回来的text内容加上<pre>text</pre>前后缀
                    //本例中设定上传文件完毕后,服务端会返回给前台[0`filepath]
                    if (data.substring(0, 1) == 0) {
                        $('#img').attr("src", $.getProjectName() + "/baseBlackArchivesInfo/readImage.htm?v="+Math.random()+"&uploadPath=" + data.substring(2));
                        $('#drvHeadPath').val(data.substring(2));
                    }
                    console.info($('#drvHeadPath').val());
                }else{
                    var data1 = JSON.parse(data);
                    $.messager.alert("错误", data1.msg, "error");
                }
            },
			error:function(data){
                $.messager.alert("错误", "上传图片失败", "error");
			}
		});
	};





});